package com.example.serviceheadlines.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.serviceheadlines.mapper.UserHeadlinesCollectionMapper;
import com.example.serviceheadlines.service.HeadlinesService;
import com.example.serviceheadlines.service.IUserHeadlinesCollectionService;
import com.tangyuan.common.result.Result;
import com.tangyuan.model.Headlines;
import com.tangyuan.model.UserHeadlinesCollection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Queue;

/**
 * @Description: user_headlines_collection
 * @Author: jeecg-boot
 * @Date: 2024-03-19
 * @Version: V1.0
 */
@Service
@Slf4j
public class UserHeadlinesCollectionServiceImpl extends ServiceImpl<UserHeadlinesCollectionMapper, UserHeadlinesCollection> implements IUserHeadlinesCollectionService {

    @Autowired
    private HeadlinesService headlinesService;

    @Override
    public Result<String> headlinesCollection(UserHeadlinesCollection userHeadlinesCollection) {
        UserHeadlinesCollection headlinesCollection = this.getOne(new LambdaQueryWrapper<UserHeadlinesCollection>()
                .eq(UserHeadlinesCollection::getUserId, userHeadlinesCollection.getUserId())
                .eq(UserHeadlinesCollection::getHeadlinesId, userHeadlinesCollection.getHeadlinesId())
                .eq(UserHeadlinesCollection::getIsDeleted, 0));
        Headlines headlines = headlinesService.getById(userHeadlinesCollection.getHeadlinesId());
        if (headlinesCollection != null) {
            this.removeById(headlinesCollection.getId());
            headlines.setCollectionNum(headlines.getCollectionNum() - 1);
        } else {
            userHeadlinesCollection.setCollectionDate(new Date());
            userHeadlinesCollection.setCreateTime(new Date());
            this.save(userHeadlinesCollection);
            headlines.setCollectionNum(headlines.getCollectionNum() + 1);
        }
        headlinesService.updateById(headlines);
        return Result.ok("操作成功！");
    }

    @Override
    public List<UserHeadlinesCollection> getMyCollectArticle(Long userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<UserHeadlinesCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserHeadlinesCollection::getUserId,userId);
        queryWrapper.lambda().orderByDesc(UserHeadlinesCollection::getCreateTime);
        return baseMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper).getRecords();
    }

    @Override
    public void deleteHeadlines(List<Long> headlinesIds) {
        if(CollectionUtils.isEmpty(headlinesIds)){
            log.info("百家收藏数据删除,百家ID为null");
            return;
        }
        baseMapper.delete(new QueryWrapper<UserHeadlinesCollection>()
                .lambda().in(UserHeadlinesCollection::getHeadlinesId,headlinesIds));
    }
}
